package com.networkviwer.tasks;

import com.networkviwer.utils.CommonUtil;
import com.networkviwer.utils.NetstatsDatasourceUtil;
import com.networkviwer.utils.ProcessDatasourceUtil;
import com.networkviwer.utils.TasklistDatasourceUtil;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class AutoDataTask implements Runnable {

    public void run() {
        Thread netstatUpdate = new Thread(() -> {
            while(true){
                try{
                    Process process = Runtime.getRuntime().exec("netstat -ano");
                    InputStream fis = process.getInputStream();
                    InputStreamReader isr = new InputStreamReader(fis,"GB2312");
                    BufferedReader br = new BufferedReader(isr);
                    Map<String,String> valueMap = new HashMap<>();
                    Connection connection = NetstatsDatasourceUtil.getConnection();
                    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                    NetstatsDatasourceUtil.deleteAllData(statement);
                    String line;
                    int i = 0;
                    while ((line = br.readLine()) != null) {
                        if (i >= 4 && line.length()<=90){
                            valueMap.put("protocol",line.substring(0,8).trim());
                            valueMap.put("local_address",line.substring(9,31).trim());
                            valueMap.put("external_address",line.substring(32,54).trim());
                            valueMap.put("status",line.substring(55,70).trim());
                            valueMap.put("pid",line.substring(71).trim());
                            NetstatsDatasourceUtil.insert(statement,valueMap);
                        }
                        i++;
                    }
                    Thread.sleep(5000);
                }catch(Throwable t){
                    t.printStackTrace();
                }
            }
        });
        Thread taskUpdate = new Thread(() -> {
            while(true){
                try{
                    Process process = Runtime.getRuntime().exec("tasklist");
                    InputStream fis = process.getInputStream();
                    InputStreamReader isr = new InputStreamReader(fis,"GB2312");
                    BufferedReader br = new BufferedReader(isr);
                    Map<String,String> valueMap = new HashMap<>();
                    Connection connection = TasklistDatasourceUtil.getConnection();
                    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                    TasklistDatasourceUtil.deleteAllData(statement);
                    String line;
                    int i = 0;
                    while ((line = br.readLine()) != null) {
                        if (i >= 3){
                            int count = CommonUtil.countCN(line);
                            valueMap.put("image",line.substring(0,25-count).trim());
                            valueMap.put("pid",line.substring(26-count,34-count).trim());
                            valueMap.put("session",line.substring(35-count,51-count).trim());
                            valueMap.put("session_num",line.substring(52-count,63-count).trim());
                            valueMap.put("memory_usage",line.substring(64-count).trim());
                            TasklistDatasourceUtil.insert(statement,valueMap);
                        }
                        i++;
                    }
                    Thread.sleep(5000);
                }catch(Throwable t){
                    t.printStackTrace();
                }
            }
        });
        Thread processUpdate = new Thread(() -> {
            while(true){
                try{
                    Process process = Runtime.getRuntime().exec("wmic process get name,executablepath,processid");
                    InputStream fis = process.getInputStream();
                    InputStreamReader isr = new InputStreamReader(fis,"GB2312");
                    BufferedReader br = new BufferedReader(isr);
                    Map<String,String> valueMap = new HashMap<>();
                    Connection connection = ProcessDatasourceUtil.getConnection();
                    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                    ProcessDatasourceUtil.deleteAllData(statement);
                    String line;
                    int i = 0;
                    int symbolName = 0;
                    int symbolProcessId = 0;
                    while ((line = br.readLine()) != null) {
                        if (i == 0){
                            symbolName = line.indexOf("Name");
                            symbolProcessId = line.indexOf("ProcessId");
                        }
                        if (i >= 2 && !line.equals("")){
                            int count = CommonUtil.countCN(line);
                            valueMap.put("executablePath",line.substring(0,symbolName-1-count).trim());
                            valueMap.put("name",line.substring(symbolName-count,symbolProcessId-1-count).trim());
                            valueMap.put("pid",line.substring(symbolProcessId-count).trim());
                            ProcessDatasourceUtil.insert(statement,valueMap);
                        }
                        i++;
                    }
                    Thread.sleep(5000);
                }catch(Throwable t){
                    t.printStackTrace();
                }
            }
        });
        netstatUpdate.start();
        taskUpdate.start();
        processUpdate.start();
    }

}
